home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 15 / CU Amiga Magazine's Super CD-ROM 15 (1997)(EMAP Images)(GB)[!][issue 1997-10].iso / CUCD / Graphics / Ghostscript / source / gdev8bcm.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-28  |  2.5 KB  |  77 lines

  1. /* Copyright (C) 1994 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of Aladdin Ghostscript.
  4.   
  5.   Aladdin Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author
  6.   or distributor accepts any responsibility for the consequences of using it,
  7.   or for whether it serves any particular purpose or works at all, unless he
  8.   or she says so in writing.  Refer to the Aladdin Ghostscript Free Public
  9.   License (the "License") for full details.
  10.   
  11.   Every copy of Aladdin Ghostscript must include a copy of the License,
  12.   normally in a plain ASCII text file named PUBLIC.  The License grants you
  13.   the right to copy, modify and redistribute Aladdin Ghostscript, but only
  14.   under certain conditions described in the License.  Among other things, the
  15.   License requires that the copyright notice and this notice be preserved on
  16.   all copies.
  17. */
  18.  
  19. /* gdev8bcm.c */
  20. /* Dynamic color mapping for 8-bit displays */
  21. #include "gx.h"
  22. #include "gxdevice.h"
  23. #include "gdev8bcm.h"
  24.  
  25. /* Initialize an 8-bit color map. */
  26. void
  27. gx_8bit_map_init(gx_8bit_color_map *pcm, int max_count)
  28. {    int i;
  29.     pcm->count = 0;
  30.     pcm->max_count = max_count;
  31.     for ( i = 0; i < gx_8bit_map_size; i++ )
  32.       pcm->map[i].rgb = gx_8bit_no_rgb;
  33. }
  34.  
  35. /* Look up a color in an 8-bit color map. */
  36. /* Return <0 if not found. */
  37. int
  38. gx_8bit_map_rgb_color(const gx_8bit_color_map *pcm, gx_color_value r,
  39.   gx_color_value g, gx_color_value b)
  40. {    ushort rgb = gx_8bit_rgb_key(r, g, b);
  41.     const gx_8bit_map_entry *pme =
  42.       &pcm->map[(rgb * gx_8bit_map_spreader) % gx_8bit_map_size];
  43.     for ( ; ; pme++ )
  44.       {    if ( pme->rgb == rgb )
  45.           return pme->index;
  46.         else if ( pme->rgb == gx_8bit_no_rgb )
  47.           break;
  48.       }
  49.     if ( pme != &pcm->map[gx_8bit_map_size] )
  50.       return pme - &pcm->map[gx_8bit_map_size];
  51.     /* We ran off the end; wrap around and continue. */
  52.     pme = &pcm->map[0];
  53.     for ( ; ; pme++ )
  54.       {    if ( pme->rgb == rgb )
  55.           return pme->index;
  56.         else if ( pme->rgb == gx_8bit_no_rgb )
  57.           return pme - &pcm->map[gx_8bit_map_size];
  58.       }
  59. }
  60.  
  61. /* Add a color to an 8-bit color map after an unsuccessful lookup, */
  62. /* and return its index.  Return <0 if the map is full. */
  63. int
  64. gx_8bit_add_rgb_color(gx_8bit_color_map *pcm, gx_color_value r,
  65.   gx_color_value g, gx_color_value b)
  66. {    int index;
  67.     gx_8bit_map_entry *pme;
  68.     if ( gx_8bit_map_is_full(pcm) )
  69.       return -1;
  70.     index = gx_8bit_map_rgb_color(pcm, r, g, b);
  71.     if ( index >= 0 )            /* shouldn't happen */
  72.       return index;
  73.     pme = &pcm->map[-index];
  74.     pme->rgb = gx_8bit_rgb_key(r, g, b);
  75.     return (pme->index = pcm->count++);
  76. }
  77.